#include "hud_item_ambient.h"

HUD_ITEM_AMBIENT::HUD_ITEM_AMBIENT(CASH *cash):QObject(), QGraphicsItemGroup()
{

    this->cash = cash;

    load();
    standart();

    connect(prop_anim_red, SIGNAL(finished()), this, SLOT(standart()));

    connect(prop_anim_blue, SIGNAL(finished()), this, SLOT(standart()));
}

QPainterPath HUD_ITEM_AMBIENT::shape() const
{
    return QPainterPath();
}

QRectF HUD_ITEM_AMBIENT::boundingRect() const
{
    return QRectF();
}

void HUD_ITEM_AMBIENT::load()
{
    item_black = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_HUD_PATH + "ambient/black.png"), this);
    item_blue = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_HUD_PATH + "ambient/blue.png"), this);
    item_red = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_HUD_PATH + "ambient/red.png"), this);

    item_black->setPos(0, 0);
    item_blue->setPos(0, 0);
    item_red->setPos(0, 0);

    item_black->setZValue(0);
    item_red->setZValue(1);
    item_blue->setZValue(2);

// init prop anims

//blue
    prop_anim_blue = new QPropertyAnimation(this, "transparent", this);

    prop_anim_blue->setLoopCount(1);
    prop_anim_blue->setDuration(1000);

    prop_anim_blue->setKeyValueAt(0, 0.4);
    prop_anim_blue->setKeyValueAt(0.5, 0.8);
    prop_anim_blue->setKeyValueAt(1, 0.4);

//red
    prop_anim_red = new QPropertyAnimation(this, "transparent", this);

    prop_anim_red->setLoopCount(1);
    prop_anim_red->setDuration(1500);

    prop_anim_red->setKeyValueAt(0, 0.5);
    prop_anim_red->setKeyValueAt(0.4, 0.9);
    prop_anim_red->setKeyValueAt(0.5, 0.6);
    prop_anim_red->setKeyValueAt(0.6, 0.9);
    prop_anim_red->setKeyValueAt(1, 0.5);

//black
    prop_anim_black = new QPropertyAnimation(this, "transparent", this);

    prop_anim_black->setLoopCount(1);
    prop_anim_black->setStartValue(0.5);
    prop_anim_black->setEndValue(1);
}

void HUD_ITEM_AMBIENT::damage()
{
    item_blue->setVisible(false);
    item_red->setVisible(true);
    item_black->setVisible(false);

    prop_anim_blue->stop();

    prop_anim_red->setCurrentTime(0);
    prop_anim_red->start();
}

void HUD_ITEM_AMBIENT::shuttle_destroyed()
{
    item_blue->setVisible(false);
    item_red->setVisible(true);
    item_black->setVisible(false);

    prop_anim_blue->stop();

    prop_anim_red->setCurrentTime(0);
    prop_anim_red->setLoopCount(-1);
    prop_anim_red->start();
}

void HUD_ITEM_AMBIENT::standart()
{
    item_blue->setVisible(false);
    item_red->setVisible(false);
    item_black->setVisible(true);

    prop_anim_red->stop();
    prop_anim_blue->stop();

    prop_anim_black->start();
}

void HUD_ITEM_AMBIENT::repair()
{
    item_blue->setVisible(true);
    item_red->setVisible(false);
    item_black->setVisible(false);

    prop_anim_red->stop();

    prop_anim_blue->setCurrentTime(0);
    prop_anim_blue->start();
}

HUD_ITEM_AMBIENT::~HUD_ITEM_AMBIENT()
{
}

//func for properties
qreal HUD_ITEM_AMBIENT::Transparent()
{
    return this->opacity();
}

void HUD_ITEM_AMBIENT::setTransparent(qreal value)
{
    this->setOpacity(value);
}
